package com.ahy231.controller;

import com.ahy231.dto.*;
import com.ahy231.service.*;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2021/8/2 15:49
 * @description 广告
 */
@Api(tags = "广告")
@RestController
@RequestMapping("/adv")
public class AdvertiseController {

    private final AdvertiseService advertiseService;

    public AdvertiseController(AdvertiseService advertiseService) {
        this.advertiseService = advertiseService;
    }

    @ApiOperation("发布广告")
    @RequiresRoles(value = {"driver", "worksite", "company"}, logical = Logical.OR)
    @PostMapping(value = "/publish", consumes = {"application/json"})
    public Result publish(@ApiParam("广告信息") @RequestBody Advertisement advertisement) {
        try {
            return new Result(ResultCode.SUCCESS, advertiseService.publish(advertisement));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看自己的广告")
    @RequiresRoles(value = {"driver", "worksite", "company"}, logical = Logical.OR)
    @PostMapping(value = "/getAdv", consumes = {"application/x-www-form-urlencoded"})
    public Result getAdvertisement(@ApiParam("页号") @RequestParam Integer pageNum, @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, advertiseService.getAdv(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看自己的接单")
    @RequiresRoles(value = {"driver", "worksite", "company"}, logical = Logical.OR)
    @PostMapping(value = "/getTx", consumes = {"application/x-www-form-urlencoded"})
    public Result getTransaction(@ApiParam("页号") @RequestParam Integer pageNum, @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, advertiseService.getAllTx(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("接单")
    @RequiresRoles(value = {"driver", "worksite", "company"}, logical = Logical.OR)
    @PostMapping(value = "/accept", consumes = {"application/x-www-form-urlencoded"})
    public Result accept(@ApiParam("交易 id") @RequestParam Long transactionId) {
        try {
            return new Result(ResultCode.SUCCESS, advertiseService.accept(transactionId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看工地广告")
    @RequiresRoles(value = {"driver", "worksite", "company"}, logical = Logical.OR)
    @PostMapping(value = "/getWorksiteAdvs", consumes = {"application/x-www-form-urlencoded"})
    public Result getWorksiteAdvs(@ApiParam("页号") @RequestParam Integer pageNum, @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, advertiseService.getWorksiteAdv(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看车辆广告")
    @RequiresRoles(value = {"driver", "worksite", "company"}, logical = Logical.OR)
    @PostMapping(value = "/getTruckAdvs", consumes = {"application/x-www-form-urlencoded"})
    public Result getTruckAdvs(@ApiParam("页号") @RequestParam Integer pageNum, @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, advertiseService.getTruckAdv(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
